package com.concerneyes.mine;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.mine.TabActivity.MyAccount;
import com.concerneyes.mine.TabActivity.MyDeposit;
import com.concerneyes.mine.TabActivity.MyExtension;
import com.concerneyes.mine.TabActivity.MyInfo;
import com.concerneyes.mine.TabActivity.OrderView.MyOrderActivity;
import com.concerneyes.mine.TabActivity.MyServiceCentre;
import com.concerneyes.mine.TabActivity.MySetting;
import com.concerneyes.mine.TabActivity.MyUserControl;
import com.concerneyes.mine.Task.MyInfoImpl;
import com.concerneyes.mine.Task.MyInfoListener;
import com.concerneyes.mine.adapter.MeListAdapter;
import com.concerneyes.mine.adapter.MyAccountAdapter;
import com.concerneyes.mine.model.MyInfoModel;

import java.util.HashMap;

import common.AbsBaseView;
import common.PreferUtil;
import http.HttpAsyncTask;
import http.JsonMap;
import util.ActivityUtil;
import util.ImageUtil;
import view.CircleImageView;
import view.ListViewForScrollView;


/**
 * 我的 View
 */
public class MineView extends AbsBaseView implements View.OnClickListener, AdapterView.OnItemClickListener, MyInfoListener {

    private ListViewForScrollView lvMe;
    private MeListAdapter adapter;
    private MyAccountAdapter adapter1;
    private Context mContext;
    private LinearLayout ll_detail, ll_yajin;
    private MyInfoImpl impl;
    private CircleImageView ivHander;
    private TextView tvName;


    public MineView(Context context) {
        super(context);
        this.mContext = context;
        initView();
        loadData(null);
    }

    @Override
    public int layoutResId() {
        return R.layout.view_mine;
    }

    public void initView() {
        ll_detail = (LinearLayout) findViewById(R.id.ll_detail);
        ll_yajin = (LinearLayout) findViewById(R.id.ll_yajin);
        ll_detail.setOnClickListener(this);
        ll_yajin.setOnClickListener(this);
        lvMe = (ListViewForScrollView) findViewById(R.id.lv_me);
        adapter = new MeListAdapter(mContext);
        lvMe.setAdapter(adapter);
        lvMe.setOnItemClickListener(this);
        ViewTreeObserver vto = lvMe.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                lvMe.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                lvMe.getHeight();
                lvMe.getWidth();
            }
        });
        ivHander = (CircleImageView) findViewById(R.id.headView_me);
        tvName = (TextView) findViewById(R.id.tv_me);
    }

    @Override
    public void loadData(Object obj) {
        impl = new MyInfoImpl(this);
        impl.myInfoImpl();
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_detail:
                ActivityUtil.setIntent(mContext, MyInfo.class, null);
                break;
            case R.id.ll_yajin:
                ActivityUtil.setIntent(mContext, MyDeposit.class, null);
                break;
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String[] title = getResources().getStringArray(R.array.mine_item_title);

        Bundle bundle = new Bundle();
        bundle.putString("title", title[position]);
        switch (position) {
            case 0:
                ActivityUtil.setIntent(mContext, MyAccount.class, bundle);
                break;
            case 1:
                ActivityUtil.setIntent(mContext, MyOrderActivity.class, bundle);
                break;
            case 2:
                ActivityUtil.setIntent(mContext, MyUserControl.class, bundle);
                break;
            case 4:
                ActivityUtil.setIntent(mContext, MyExtension.class, bundle);
                break;
            case 5:
                ActivityUtil.setIntent(mContext, MyServiceCentre.class, bundle);
                break;
            case 7:
                ActivityUtil.setIntent(mContext, MySetting.class, bundle);
                break;
        }

    }

    @Override
    public void onMyInfoSuccess(MyInfoModel data) {
        String resLink = CEApp.getResLink(data.getPortrait());
        if (TextUtils.isEmpty(PreferUtil.getHeaderImgUrl())) {
            ImageUtil.glideloadImage(resLink, ivHander, R.drawable.default_header);
        } else {
            ImageUtil.glideloadImage(PreferUtil.getHeaderImgUrl(), ivHander, R.drawable.default_header);
        }
        tvName.setText(data.getNickname());
    }

    @Override
    public void onMyInfoFail() {

    }

    @Override
    public void onMyInfoUpdateSuccess(MyInfoModel model) {

    }

    @Override
    public void onMyInfoUpdateFail() {

    }
}
